package com.csust.travel.controller;


import com.auth0.jwt.JWT;
import com.csust.travel.entity.*;
import com.csust.travel.mapper.NewUsersMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.annotation.ElementType;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/newusers")
public class NewUsersController {
    @Autowired
    NewUsersMapper newUsersMapper;


    @RequestMapping("/page")
    public ResBean page(@RequestBody pageDate pageData) {
        PageHelper.startPage(pageData.getCurrentPage(), pageData.getPageSize());
        List<NewUsers> Items;
        String queryString =pageData.getQueryString();
        String na = pageData.getRole();
        if ((isEmptyString(queryString)) && (isEmptyString(na))) {
            Items = newUsersMapper.findAllItem();
        } else if (isEmptyString(queryString) && !isEmptyString(na)) {
            Items = newUsersMapper.findUsersByRole(na);
        } else if (!isEmptyString(queryString) && isEmptyString(na)) {
            Items = newUsersMapper.findByQuery(queryString);
        } else {
            Items = newUsersMapper.findByFilters(queryString, na);
        }
        PageInfo<NewUsers> newUsersPageInfo = new PageInfo<>(Items);
        return new ResBean(true, null, newUsersPageInfo);
    }

    public boolean isEmptyString(String s) {
        return (s == null) || s.trim().equals("");
    }

    @RequestMapping("/all")
    public ResBean findOne(String id) {
       String userId = JWT.decode(id).getSubject();
        int intUserId = Integer.parseInt(userId);
        NewUsers item = newUsersMapper.selectById(intUserId);
        try {
            return new ResBean(true, null, item);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBean(false, null);
        }
    }
    @RequestMapping("/avatar")
    public ResBean findTwo( @RequestBody UserParam param1) {

        Integer id = param1.getId();
        String avatar = param1.getAvatar();
       newUsersMapper.setAvatar(avatar,id);
        try {
            return new ResBean(true, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBean(false, null);
        }
    }

    @RequestMapping("/updatePasswd")
    public ResBean findThree( @RequestBody UserParam2 param2) {

        Integer id = param2.getId();
        String newPasswd = param2.getNewPasswd();

        newUsersMapper.UpdatePasswd(newPasswd,id);
        try {
            return new ResBean(true, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBean(false, null);
        }
    }


    @RequestMapping("/save")
    public ResBean editItem(@RequestBody NewUsers item) {
        try {
            newUsersMapper.editItem(item);
            return new ResBean(true, "编辑成功666");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false, "编辑失败666");
    }




    @RequestMapping("/del")
    public ResBean delItem(Integer id) {
        try {
            newUsersMapper.delItem(id);
            return new ResBean(true, "删除成功666");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false, "删除失败");
    }

    @RequestMapping("/login")
    public  ResBean login(@RequestBody NewUsers loginForm) {
        NewUsers newUsers = newUsersMapper.findUserByUsername(loginForm.getUsername());
        if (newUsers == null) {
            return new ResBean(false, "用户不存在");
        }

        if (newUsers.getPassword().equals(loginForm.getPassword())) {
            switch (newUsers.getRole()) {
                case "用户":
                    return new ResBean(true, "用户 " + newUsers.getUsername() + ", 登陆成功");
                case "管理员":
                    return new ResBean(true, "管理员 " + newUsers.getUsername() + ", 登陆成功");
                default:
                    return new ResBean(true, "访客 " + newUsers.getUsername() + ", 登陆成功");
            }
        } else {
            return new ResBean(false, "账号或密码错误");
        }
    }
}
